<?PHP
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | ECMS Site Sapiens						                              |
// +----------------------------------------------------------------------+
// | Copyright � 2003-2006 Red Graphic Systems.                           |
// |                                                                      |
// | Red Graphic Systems                                                  |
// | ul. Pervomayskaya, 24/2-108                                          |
// | Minsk 220088 BELARUS                                                 |
// | Phone/fax: +375 17 233 9009                                          |
// | Email: info@redgraphic.com                                           |
// +----------------------------------------------------------------------+
// |  This material is an object of intellectual property of the          |
// |  Red Graphic Systems company and subjects to the               	  | 
// |  proprietary right. Its whole or partial reproduction (distribution, | 
// |  copying, or use) is possible on the basis of license agreement with |
// |  the Red Graphic Systems company and only under the conditions 	  | 
// |  and aims stipulated in the license agreement.                       |
// +----------------------------------------------------------------------+
// CVS: $Id: basics.inc.php,v 1.1 2006/02/20 08:31:07 sheiko Exp $


if (!function_exists('lang')) {
/**
 * Returns string on specified lanuage.
 *
 * @param string $key
 * @return string
 */
   function lang($key) {
   	global $lang, $default_charset;
   	if(!$key) return false;
      if(!isset($lang)) {
      	if( !@include_once(ROOT_PATH."langs".$default_charset.".inc.php") )
      		@include_once(ROOT_PATH."lang/en.inc.php");
      }
      if(isset($lang[$key])) return $lang[$key];
      else return $key;
   }
}


if (!function_exists('trace')) {
	/**
	 * Show tracing log of current program position
	 *
	 * @return boolean
	 */
	function trace() {
		$backtrace = debug_backtrace(); 
		d($backtrace);
	}
}

if (!function_exists('validateURLPath')) {
	/**
	 * Validate a part of URL during submitting
	 *
	 * @param string Russian/Other Language UTF-8 string
	 * @return string Latin string
	 */	
	function validateURLPath($str) {
		$str = preg_replace("/[".preg_quote('\',*!@#$%^&()-+/\"',"/")."]/is", "", $str);
		$str = trim(preg_replace("/[\s'`]/", "_", $str));
		if(!preg_match("/[^a-zA-Z0-9_]/is",$str)) return strtolower($str);
		return strtolower(transliterate($str));
	}
}

if (!function_exists('transliterate')) {

	/**
	 * Transliteration
	 *
	 * @param string Russian/Other Language UTF-8 string
	 * @return string Latin string
	 */
	function transliterate($str) {
		
	    $src=array(
	    "Щ",  "Ш", "Ч", "Ц","Ю", "Я", "Ж", "А","Б","В","Г","Д","Е","Ё","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х", "Ь","Ы","Ъ","Э","Є","Ї",
	    "Č", "Ð", "Ġ", "Ğ", "Ñ", "Š", "Þ" , "Ž",
	    "À", "Â", "Ç", "É", "È", "Ê", "Ë", "Î", "Ï", "Ô", "Û", "Ù", "Ü",
	    "щ",  "ш", "ч", "ц","ю", "я", "ж", "а","б","в","г","д","е","ё","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х", "ь","ы","ъ","э","є","ї",
	    "č", "ð", "ġ", "ğ", "ñ", "š", "þ" , "ž", 
	    "à", "â", "ç", "é", "è", "ê", "ë", "î", "ï", "ô", "û", "ù", "ü",
	    );
	    
	    $lat=array(
	    "Shh","Sh","Ch","C","Ju","Ja","Zh","A","B","V","G","D","Je","Jo","Z","I","J","K","L","M","N","O","P","R","S","T","U","F","Kh","'","Y","`","E","Je","Ji",
	    "CH", "THE", "G", "G", "NY", "SH", "THIN", "J",
	    "A", "A", "CH", "E", "E", "E", "E", "I", "I", "O", "U", "U", "U",
	    "shh","sh","ch","c","ju","ja","zh","a","b","v","g","d","je","jo","z","i","j","k","l","m","n","o","p","r","s","t","u","f","kh","'","y","`","e","je","ji",
	    "ch", "the", "g", "g", "ny", "sh", "thin", "j",
	    "a", "a", "ch", "e", "e", "e", "e", "i", "i", "o", "u", "u", "u",
	    );
	    
	    for($i=0; $i<count($src); $i++){
	        $c_cyr = $src[$i];
	        $c_lat = $lat[$i];
	        $str = str_replace($c_cyr, $c_lat, $str);
	    }
	      $str = preg_replace("/([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM]+)[jJ]e/", "\${1}e", $str);
	      $str = preg_replace("/([qwrtpsdfghklzxcvbnmQWRTPSDFGHKLZXCVBNM]+)[jJ]/", "\${1}'", $str);
	      $str = preg_replace("/([eyuioaEYUIOA]+)[Kk]h/", "\${1}h", $str);
	      $str = preg_replace("/^kh/", "h", $str);
	      $str = preg_replace("/^Kh/", "H", $str);    		
		  $str = preg_replace("/[\s'`]/", "_", $str);
		return $str;
	}
}

function array2table($Arr) {
	if(!$Arr) return false;
	$res = '
	<table border="0" cellspacing="0" style="border-left: 1px solid silver; border-top: 1px solid silver;">'."\n";
	$res .='<tr>';
	$Key = each($Arr);
	foreach($Arr[$Key["key"]] as $Index=>$Cell) {
			$res .='<td style="border-right: 1px solid silver; border-bottom: 1px solid silver;" bgcolor="#EEEEEE">'.$Index.'</td>';
		}
	$res .='<tr>';	
	foreach($Arr as $Row) {
		$res .='<tr>';
		foreach($Row as $Index=>$Cell) {
			if(!is_array($Cell) and !is_object($Cell)) {
				if(preg_match("/date/is", $Index)) $nowrap='nowrap="nowrap"'; else $nowrap='';
				$res .='<td '.$nowrap.' style="border-right: 1px solid silver; border-bottom: 1px solid silver;">'.($Cell?$Cell:"&nbsp;").'</td>';
			}
		}
		$res .='</tr>'."\n";
	}
	$res .='</table>';
	return $res;
}

if (!function_exists('toLog')) {
	/**
	 * Get var dump as a string
	 *
	 * @param $Var
	 * @return boolean
	 */
	function print_r_log($Var)	{
		ob_start();
		print_r($Var);
		$ret_str = ob_get_contents();
		ob_end_clean();
		return $ret_str; 
	}

	/**
	 * Retrieve all different elements
	 *
	 * @param $Message
	 * @return boolean
	 */
	function toLog( $Message=false, $Method="w" ) {
		if(is_array($Message) or is_object($Message)) $Message = print_r_log($Message); 
		$fp=fopen(ROOT_PATH."log/error.log.txt", $Method);
		fwrite($fp, "LOG ".date("Y-m-d H:i:s").":\r\n".$Message."\r\n" );
		fclose($fp);		
		return true;
	}
}


if (!function_exists('validateXML')) {
	/**
	 * Validate XML entry
	 *
	 * @param string $Content
	 * @param string type "Element" OR "Attribute"
	 * @return boolean
	 */
	function validateXMLEntry($Content, $Type="element")	{
		if($Type=="element") 
			$data = "<item>{$Content}</item>";
		else 
			$data = "<item attr=\"{$Content}\">empty</item>";
			
		$xml_parser = xml_parser_create(); 
		   if (!@xml_parse($xml_parser, $data)) return false;
		@xml_parser_free($xml_parser); 
		return true;
	}
	/**
	 * Validate XML code
	 *
	 * @param string $Content
	 * @return boolean
	 */
	function validateXML($data)	{
		$xml_parser = xml_parser_create(); 
		   if (!@xml_parse($xml_parser, $data)) return false;
		@xml_parser_free($xml_parser); 
		return true;
	}	
}

if (!function_exists('decode')) {
	function decode($arr){
		$unicode=array("0410","0411","0412","0413","0414","0415","0416","0417","0418","0419","041A","041B","041C","041D","041E","041F","0420","0421","0422","0423","0424","0425","0426","0427","0428","0429","042A","042B","042C","042D","042E","042F","0430","0431","0432","0433","0434","0435","0436","0437","0438","0439","043A","043B","043C","043D","043E","043F","0440","0441","0442","0443","0444","0445","0446","0447","0448","0449","044A","044B","044C","044D","044E","044F");
		$cyr=array("А","Б","В","Г","Д","Е","Ж","З","И","Й","К","Л","М","Н","О","П","Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Щ","Ъ","Ы","Ь","Э","Ю","Я","а","б","в","г","д","е","ж","з","и","й","к","л","м","н","о","п","р","с","т","у","ф","х","ц","ч","ш","щ","ъ","ы","ь","э","ю","я");
		$unicode=array_flip($unicode);
		if($cyr[$unicode[$arr[1]]])	return $cyr[$unicode[$arr[1]]]; else return $arr[0];
	}
}
?>